//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.member;

import com.aidong.dao.wills.MemberAmortizeDao;
import com.aidong.model.wills.MemberAmortize;
import com.aidong.service.MemberAmortizeService;
import com.aidong.util.ExportXls;
import com.aidong.web.controller.BaseController;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"memberAmortize"})
public class MemberAmortizeController extends BaseController {
    @Resource
    MemberAmortizeDao memberAmortizeDao;
    @Resource
    MemberAmortizeService memberAmortizeService;

    public MemberAmortizeController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return (new ModelAndView("member/amortize/list")).addObject("dt1", sdf.format(new Date(System.currentTimeMillis() - 86400000L))).addObject("dt2", sdf.format(new Date()));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "date",required = false) String date, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        List list = null;
        if(!this.chkNull(date) && date.length() > 7) {
            String mv = date.substring(0, 4) + date.substring(5, 7);
            list = this.memberAmortizeDao.getShopSum(mv);
        }

        ModelAndView mv1 = new ModelAndView("member/amortize/_list");
        mv1.addObject("list", list);
        return mv1;
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "date",required = false) String date, HttpServletResponse response) {
        try {
            List e = null;
            if(!this.chkNull(date) && date.length() > 7) {
                String xls = date.substring(0, 4) + date.substring(5, 7);
                e = this.memberAmortizeDao.getShopSum(xls);
            }

            ExportXls xls1 = new ExportXls(response, "会籍摊销汇总", "会籍摊销汇总");
            xls1.addHead(new String[]{"门店编号", "门店名称", "摊销金额"});
            xls1.setWidth(new int[]{10, 15, 12});
            if(e != null) {
                Iterator i$ = e.iterator();

                while(i$.hasNext()) {
                    MemberAmortize amortize = (MemberAmortize)i$.next();
                    xls1.addRow(new Object[]{amortize.getShopCode(), this.cache.getValue("shopName", amortize.getShopCode()), amortize.getAmount()});
                }
            }

            xls1.close();
            this.addUserLog("导出会籍摊销汇总", (e == null?0:e.size()) + "条记录");
        } catch (Exception var8) {
            this.log.error("import", var8);
        }

        return null;
    }

    @RequestMapping({"_detail"})
    @ResponseBody
    public String _detail(@RequestParam(value = "date",required = false) String date) {
        if(!this.chkNull(date) && date.length() > 7) {
            String period = date.substring(0, 4) + date.substring(5, 7);
            return this.memberAmortizeService.createAmortizeDetail(period)?this.getResult(true, "生成完成"):this.getResult(false, "发生错误");
        } else {
            return this.getResult(false, "请指定日期");
        }
    }

    @RequestMapping({"detail"})
    public ModelAndView detail(@RequestParam(value = "date",required = false) String date, HttpServletResponse response) {
        try {
            if(!this.chkNull(date) && date.length() > 7) {
                String e = date.substring(0, 4) + date.substring(5, 7);
                String fn = this.getSetting("downloadPath") + e.substring(0, 4) + File.separator + "amortize" + File.separator + "amortize_" + e + ".xlsx";
                File file = new File(fn);
                if(!file.exists()) {
                    return this.showAlert("报表文件未生成");
                }

                response.sendRedirect("/download/" + e.substring(0, 4) + "/amortize/amortize_" + e + ".xlsx");
            }
        } catch (Exception var6) {
            this.log.error("amortize detail", var6);
        }

        return null;
    }
}
